package org.langqiao.appproject.俄罗斯方块;

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.TitledBorder;

import java.util.*; 
import java.awt.BorderLayout; 

public class Calendar extends JPanel implements ActionListener { 
	public static final String HOUR_OF_DAY = null;
	//定义dd
	JComboBox Month = new JComboBox();  
	JComboBox Year = new JComboBox();  
	JLabel Year_l = new JLabel("年"); 
	JLabel Month_l = new JLabel("月"); 	
	Date now_date = new Date(); 
	JLabel[] Label_day = new JLabel[49]; 	

	int now_year = now_date.getYear() + 1900;
	int now_month = now_date.getMonth();
	boolean bool = false;
	String year_int = null; 
	int month_int; 
	JPanel pane_ym = new JPanel(); 
	JPanel pane_day = new JPanel(); 
	
	public Calendar() {  
		super();
		//设定年月 
		for (int i = now_year - 10; i <= now_year + 20; i++) { 
			Year.addItem(i + "");
		} 

		for (int i = 1; i < 13; i++) {
			Month.addItem(i + ""); 
		} 

		Year.setSelectedIndex(10); 
		pane_ym.add(new  JLabel("        "));
		pane_ym.add(Year);
		pane_ym.add(Year_l);	
		Month.setSelectedIndex(now_month);		
		pane_ym.add(Month); 
		pane_ym.add(Month_l); 
		pane_ym.add(new  JLabel("        "));
		
		Month.addActionListener(this); 
		Year.addActionListener(this);  
		
		
		//初始化日期并绘制 
		pane_day.setLayout(new GridLayout(7, 7, 10, 10)); 
		for (int i = 0; i < 49; i++) { 
			Label_day[i] = new JLabel(" "); 
			pane_day.add(Label_day[i]);
		} 

		this.setDay();

		this.setLayout(new BorderLayout()); 
//		setContentPane(pane_day); 
//		setContentPane(pane_ym); 
//		setContentPane(pane_parent); 
	    this.add(pane_day, BorderLayout.CENTER); 
		this.add(pane_ym, BorderLayout.NORTH); 
		this.setSize(100,200);
		this.setBorder(new TitledBorder("日历"));

		setSize(300,300);
		

	} 

	void setDay() { 
		if (bool) {
			year_int = now_year + ""; 
			month_int = now_month; 
		} 
		else { 
			year_int = Year.getSelectedItem().toString(); 
			month_int = Month.getSelectedIndex();
		} 
		
		int year_sel = Integer.parseInt(year_int) - 1900; //获得年份值 
		Date dt = new Date(year_sel, month_int, 1); //构造一个日期 
		GregorianCalendar cal = new GregorianCalendar(); //创建一个Calendar实例 
		cal.setTime(dt); 

		String week[] = { "日", "一","二", "三", "四", "五", "六" }; 
		
		int day = 0;
		int day_week = 0; 

		for (int i = 0; i < 7; i++) {
			Label_day[i].setText(week[i]); 
		} 


		//月份
		if (month_int == 0||month_int == 2 ||month_int == 4 ||
				month_int == 6 || 
				month_int == 9 ||month_int == 11) { 
			day = 31; 
		}
		else if (month_int == 3 ||month_int == 5 || month_int == 7||
				month_int == 8 ||month_int == 10|| month_int == 1) { 
			day = 30; 
		} else { 
			if (cal.isLeapYear(year_sel)) {
				day = 29; 
			} 
			else { 
				day = 28; 
			} 
		} 

		day_week = 7 + dt.getDay(); 
		int count = 1; 

		for (int i = day_week; i < day_week + day; count++, i++) { 
			if (i % 7 == 0 ||i == 13||i == 20||i == 27|| 
					i == 48 ||i == 34 ||i == 41) { 
				if (i == day_week + now_date.getDate() - 1) {
					Label_day[i].setForeground(Color.blue);
					Label_day[i].setText(count + "");
				} else {
					Label_day[i].setForeground(Color.red); 
					Label_day[i].setText(count + ""); 
				} 
			} else { 
				if (i == day_week + now_date.getDate() - 1) { 
					Label_day[i].setForeground(Color.blue);
					                                       
					Label_day[i].setText(count + ""); 
				} else { 
					Label_day[i].setForeground(Color.black); 
					Label_day[i].setText(count + ""); 
				} 
			} 
		} 
		if (day_week == 0) { 
			for (int i = day; i < 49; i++) { 
				Label_day[i].setText(" "); 
			} 
		} else { 
			
			for (int i = 7; i < day_week; i++) {
				Label_day[i].setText(" "); 
			} 

			for (int i = day_week + day; i < 49; i++) { 
				Label_day[i].setText("  "); 
			}
		} 
	} 
	public void actionPerformed(ActionEvent e) { 
		if (e.getSource() == Year || e.getSource() == Month) { 
			bool = false; 
			this.setDay(); 
		} 
	} 

}

